<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="keyword" placeholder="名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="keywordQuery" icon="el-icon-search">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd" icon="el-icon-edit-outline">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="systemdictionarydetails" highlight-current-row v-loading="listLoading" @selection-change="selsChange"
              style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="name" label="字典名称" width="160" sortable>
      </el-table-column>
      <el-table-column prop="systemdictionarytype.sn" label="类型编号" width="160" sortable>
      </el-table-column>
      <el-table-column label="操作">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)" icon="el-icon-edit">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)" icon="el-icon-delete-solid">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="prev, pager, next"
                     @current-change="handleCurrentChange"
                     :page-size="pageSize"
                     :current-page="pageNum"
                     :total="total"
                     style="float:right;">
      </el-pagination>
    </el-col>

    <!--新增/修改  界面-->
    <el-dialog :title="dialogTitle" :visible.sync="saveFormVisible" :close-on-click-modal="false">
      <!--
        :rules 加个校验规则
        ref 相当于取个别名
       -->
      <el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">
        <el-form-item label="字典名称" prop="name">
          <el-input v-model="saveForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="字典类型">
          <el-select v-model="saveForm.typesId" clearable placeholder="请选择">
            <el-option
                v-for="item in systemdictionarytype"
                :label="item.name"
                :value="item.id">
              <span>{{ item.name }}</span>
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="saveFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">提交</el-button>
      </div>
    </el-dialog>

  </section>
</template>

<script>
import util from '../../common/js/util'
//import NProgress from 'nprogress'
import {
  getSystemdictionarydetailListPage,
  removeSystemdictionarydetail,
  batchRemoveSystemdictionarydetail,
  editSystemdictionarydetail,
  addSystemdictionarydetail
} from '../../api/api';

export default {
  data() {
    return {
      filters: {
        name: ''
      },
      systemdictionarydetails: [], //查询出的(总)数据
      total: 0, //总条数
      pageNum: 1, //当前页
      pageSize: 5, //每一页有多少条数据
      listLoading: false, //盲等框
      sels: [],//列表选中列

      dialogTitle: '',

      //所有字典类型
      systemdictionarytype: [],

      keyword: '', //查询关键字

      saveFormVisible: false,//编辑界面是否显示
      saveLoading: false, //编辑的盲等框

      saveFormRules: {
        sn: [
          {required: true, message: '请输入部门编号', trigger: 'blur'}
        ],
        name: [
          {required: true, message: '请输入部门名称', trigger: 'blur'}
        ]
      },
      //编辑界面数据
      saveForm: {
        id: null,
        name: '',
        typesId: null,
      },
    }
  },
  methods: {
    // //性别显示转换
    // formatSex: function (row, column) {
    //   return row.state == 0 ? '正常' : row.state == 0 ? '禁用' : '未知';
    // },
    handleCurrentChange(val) {
      this.pageNum = val;
      this.getSystemdictionarydetails();
    },

    //高级查询
    keywordQuery() {
      //如果没有在第一页查询，就查不到，所以查询前先使当前页为第一页就没有问题
      this.pageNum = 1;
      this.getSystemdictionarydetails();
    },

    //获取部门列表
    getSystemdictionarydetails() {
      //分页和高级查询对象
      let para = {
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        keyword: this.keyword
        // name: this.filters.name
      };
      this.listLoading = true;

      this.$http.post("/systemdictionarydetail", para).then(res => {
        //回调成功
        this.systemdictionarydetails = res.data.rows;
        this.total = res.data.totals;
      }).catch(res => {
        console.log(res)
        this.$message({
          message: '系统繁忙，请稍后重试',
          type: 'error'
        });
      });
      //无论成功还是失败都要关闭盲等框
      this.listLoading = false;
      //NProgress.start();
      // getSystemdictionarydetailListPage(para).then((res) => {
      // 	this.total = res.data.total;
      // 	this.systemdictionarydetails = res.data.systemdictionarydetails;
      // 	this.listLoading = false;
      // 	//NProgress.done();
      // });
    },
    //删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        //NProgress.start();
        // let para = {id: row.id};
        this.$http.delete("/systemdictionarydetail/" + row.id).then(res => {
          //关闭忙等框
          this.listLoading = true;
          //如果删除成功，返回一个提示
          if (res.data.success) {
            this.$message({
              message: '删除成功',//
              type: 'success'
            });
            //重新定位到第一页
            this.pageNum = 1;
            //手动刷新列表
            this.getSystemdictionarydetails();
          }
        }).catch(res => {
          console.log(res);
          this.$message({
            message: '删除失败',
            type: 'error'
          });
        });
        // removeSystemdictionarydetail(para).then((res) => {
        //   this.listLoading = false;
        //   //NProgress.done();
        //   this.$message({
        //     message: '删除成功',
        //     type: 'success'
        //   });
        //   this.getSystemdictionarydetails();
        // });
      }).catch(() => {

      });
    },
    //显示编辑界面
    handleEdit: function (index, row) {
      this.dialogTitle = "编辑";
      this.saveFormVisible = true;
      this.saveForm = Object.assign({}, row);
      this.getManagers();
    },
    //显示新增界面
    handleAdd: function () {
      this.dialogTitle = "新增";
      this.saveFormVisible = true;
      //将
      this.saveForm = {
        id: null,
        name: '',
        typesId: null,
      };
      this.getManagers();
    },
    //新增/修改
    saveSubmit: function () {
      //获取所有的引用下的saveform的校验结果
      this.$refs.saveForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.saveLoading = true;
            //NProgress.start();
            let para = Object.assign({}, this.saveForm);
            // //拿到数组最后的值组成对象
            // var isArr = para.parent_id;
            // if (Array.isArray(isArr)) {
            //   if (isArr.length == 0) {
            //     para.parent_id = null;
            //   } else {
            //     para.parent_id = isArr[isArr.length - 1]
            //   }
            // }
            para.birth = (!para.birth || para.birth == '') ? '' : util.formatDate.format(new Date(para.birth), 'yyyy-MM-dd');
            this.$http.put("/systemdictionarydetail", para).then((res) => {
              this.saveLoading = false;
              //NProgress.done();
              let data = res.data;
              if (data) {
                this.$message({
                  message: data.msg,
                  type: 'success'
                });
              } else {
                this.$message({
                  message: data.msg,
                  type: 'error'
                });
              }
              this.$refs['saveForm'].resetFields();
              this.saveFormVisible = false;
              this.getSystemdictionarydetails();
            }).catch(res => {
              this.$message({
                message: '系统繁忙，请稍后再试！',
                type: 'error'
              });
            });
          });
        }
      });
    },
    selsChange: function (sels) {
      this.sels = sels;
    },
    //批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id);//这句什么意思？？？？？？
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        //NProgress.start();
        // let para = {ids: ids};
        this.$http.patch("/systemdictionarydetail", ids).then(res => {
          this.listLoading = false;
          //NProgress.done();
          this.$message({
            message: '删除成功',
            type: 'success'
          });
          this.pageNum = 1;
          this.getSystemdictionarydetails();
        });
      }).catch(() => {

      });
    },

    //获取所有字典类型
    getManagers() {
      this.$http.get("/systemdictionarytype").then(res => {
        this.systemdictionarytype = res.data;
      })
    },

  },
  mounted() {
    this.getSystemdictionarydetails();
  }
}

</script>

<style scoped>

</style>